package com.music.service.composer.dao.impl;


import static com.music.service.composer.constants.ComposerSQLQueries.ADD_NEW_COMPOSER;
import static com.music.service.composer.constants.ComposerSQLQueries.DELETE_COMPOSER;
import static com.music.service.composer.constants.ComposerSQLQueries.EDIT_COMPOSER_NAME;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.music.service.composer.constants.ComposerState;
import com.music.service.composer.dao.ComposerDAO;

public class ComposerDAOImpl implements ComposerDAO {
	private JdbcTemplate mMusicsTemplate ;

	
	/**
	 * @return the musicsTemplate
	 */
	public JdbcTemplate getMusicsTemplate() {
		return mMusicsTemplate;
	}

	/**
	 * @param pMusicsTemplate the musicsTemplate to set
	 */
	@Required
	public void setMusicsTemplate(JdbcTemplate pMusicsTemplate) {
		mMusicsTemplate = pMusicsTemplate;
	}

	public boolean addNewComposer(final Long pMemberId, final String pComposerName) {
		int nRowsUpdated = getMusicsTemplate().update(ADD_NEW_COMPOSER,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setLong(1, pMemberId);
						preparedStatement.setString(2, pComposerName);
						preparedStatement.setInt(3, ComposerState.ACTIVE.getState());
					}
				});
		return nRowsUpdated == 1;
	}
	
	public boolean deleteComposer(final Long pMemberId,final Long pComposerId) {
		int nRowsUpdated = getMusicsTemplate().update(DELETE_COMPOSER,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setInt(1, ComposerState.DELETED.getState());
						preparedStatement.setLong(2, pComposerId);
						preparedStatement.setLong(3, pMemberId);
						preparedStatement.setInt(4, ComposerState.VERIFIED.getState());
					}
				});
		return nRowsUpdated == 1;
	}

	public boolean editComposerName(final Long pMemberId, final Long pComposerId,final String pComposerName) {
		int nRowsUpdated = getMusicsTemplate().update(EDIT_COMPOSER_NAME,
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement preparedStatement)
							throws SQLException {
						preparedStatement.setString(1, pComposerName);
						preparedStatement.setLong(2, pComposerId);
						preparedStatement.setLong(2, pMemberId);
						preparedStatement.setInt(3, ComposerState.ACTIVE.getState());
					}
				});
		return nRowsUpdated == 1;
	}
	
	
	
	
}
